package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.content.UserType;
import com.dao.EquipDAO;
import com.dao.SignUpDAO;
import com.dao.UserDAO;
import com.model._do.EquipmentDO;
import com.model._do.SignUpDO;
import com.model._do.UserDO;
import com.model.dto.SuperviseDTO.Search.UserInfoSearch;
import com.model.dto.SuperviseDTO.AppointInfo;
import com.model.dto.SuperviseDTO.SignRelateDTO;
import com.model.dto.SuperviseDTO.UserInfoDTO;
import com.service.EquipmentService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.util.LocationUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private SignUpDAO signUpDAO;

    @Override
    public UserDO getUserByUserId(String userId) {
        return userDAO.getUserByUserId(userId);
    }

    @Override
    public UserDO saveUser(UserDO user) {
        UserDO oldUser = userDAO.getUserByUserId(user.getUserId());
        if (oldUser == null) {
            user.setUserType(UserType.PATIENT);
            userDAO.saveUser(user);
            return user;
        } else {
            return oldUser;
        }
    }

    @Override
    public boolean updateUser(UserDO user) {
        if (user.getUserName() != null) {
            userDAO.updateUserName(user);
        }
        if (user.getUserPhone() != null) {
            userDAO.updateUserPhone(user);
        }
        if (user.getUserLocation() != null) {
            userDAO.updateUserLocation(user);
        }
        if (user.getUserDiscription() != null) {
            userDAO.updateUserDiscription(user);
        }
        if (user.getUserType() != null) {
            userDAO.updateUserType(user);
        }
        return true;
    }

    @Override
    public List<UserDO> getNearDoctor(HashMap<String, String> hashMap) {
        return userDAO.selectNearDoctor(hashMap);
    }

    @Override
    public List<UserInfoDTO> getUserByType(UserInfoSearch search) {
        List<UserDO> userList = userDAO.getUserByUserType(search);
        List<UserInfoDTO> infoList = new ArrayList<>();
        for (UserDO user: userList
             ) {
            if (LocationUtil.filterLocation(JSONObject.parseObject(user.getUserLocation()), search)) {
            } else {
                continue;
            }
            UserInfoDTO info = new UserInfoDTO();
            info.setUser(user);
            String userType = search.getUserType();
            SignUpDO sign = new SignUpDO();
            sign.setAsUserType(userType);
            sign.setUserId(user.getUserId());
            info.setEquipList(equipmentService.getSupervise(sign));
            infoList.add(info);
        }
        return infoList;
    }

    
    @Override
    public List<SignRelateDTO> getUnConfirmDoctor(SignUpDO sign) {
        sign.setAsUserType("PATIENT");

        //所有此用户以普通用户身份签约的equip
        List<SignUpDO> equipIdList = signUpDAO.getByUserId(sign);
        List<SignRelateDTO> relateList = new ArrayList<>();
        equipIdList.forEach(e -> {
            //获得一个医生发来的我未确认的签约请求
            SignUpDO unSign = signUpDAO.getUnConByEquip(e.getEquipId());
            SignRelateDTO relate = new SignRelateDTO();
            if(unSign != null){
                relate.setSign(unSign);
                relate.setUser(userDAO.getUserByUserId(unSign.getUserId()));
                relate.setEquip(equipmentService.getEquipByEquipId(unSign.getEquipId()));
                relateList.add(relate);
            }
        });
        return relateList;
    }
}
